package entity;

import loading.ImageLoader;
import math.Vector2D;

/**
 * 
 * @author Greg G
 *
 * The DynamicEntity, a subclass of entity, provides methods and 
 * functionality for moving an entity
 */

public abstract class DynamicEntity extends Entity
{
	private Vector2D velocity;
	private Vector2D heading;
	private double mass;
	private Vector2D steeringForce;
	private double maxSpeed;
	
	public DynamicEntity( Vector2D pos, ImageLoader img )
	{
		super( pos, img );
		velocity = new Vector2D( 0, 0 );
		heading = new Vector2D( 0, 1 );
		mass = 1;
		steeringForce = new Vector2D( 0, 0 );
		maxSpeed = 50;
	}
	
	//update the position based on velocity
	public void update( double duration )
	{
		super.update( duration );
		
		Vector2D accel = Vector2D.divide( steeringForce, mass );
		steeringForce.setX( 0 ); steeringForce.setY( 0 );
		
		velocity.add( accel.multiply( duration ) );
		velocity.setMagnitude( maxSpeed );
		
		if( velocity.getMagnitude() > 0.000001 )
		{
			heading = Vector2D.getNormal( velocity );
			
			this.setAngle( heading.getAngle() );
		}
		
		getPosition().add( Vector2D.multiply( velocity, duration ) );
	}
	
	public void setSpeed( double speed )
	{
		velocity.setMagnitude( speed );
	}

	public void setVelocity( Vector2D vel )
	{
		velocity = vel;
	}
	
	public void setVelocity( double vx, double vy )
	{
		velocity = new Vector2D( vx, vy );
	}
	
	public Vector2D getVelocity()
	{
		return velocity;
	}
	
	public void setSteeringForce( Vector2D v )
	{
		steeringForce = v;
	}
	
	public void setMaxSpeed( double d )
	{
		maxSpeed = d;
	}
	
	public double getMaxSpeed()
	{
		return maxSpeed;
	}
}

